import Vue from "vue";
import axios from "axios";

import {
  baseURL,
  requestTimeout,
  contentType,
  UtilVar,
} from "@/config/UtilVar.js";

/**
 * @description axios初始化
 */
const instance = axios.create({
  baseURL,
  timeout: requestTimeout,
  headers: {
    "Content-Type": contentType,
  },
});
// axios.defaults.withCredentials = true;
// 添加请求拦截器
instance.interceptors.request.use(
  function (config) {
    // 在发送请求之前做些什么 传token
    let token = localStorage.getItem("token");
    // config.headers.common["Content-Type"] = "application/json;charset=utf-8";
    // config.headers.common['token'] = token;  //Authorization
    config.headers["Token"] = token;
    return config;
  },
  function (error) {
    // 对请求错误做些什么
    console.log(error);
    return Promise.reject(error);
  }
);
/**
 * @响应拦截
 */
instance.interceptors.response.use(
  (response) => {
    if (response.status !== 200) {
      return Promise.reject(response);
    }
    /**
     * @code 登录过期 token验证失败 根据后端调
     */
    if (response.data.code == UtilVar.code) {
      // router.push("/login")
    }
    return response.data;
  },
  (error) => {
    // console.log('axiosError',error);
    let err = {
      success: false,
      msg: "未知异常，请联系管理员！",
    };
    return Promise.reject(err);
  }
);

export default instance;
